<?php 

namespace app\api\controller\v1;

// use think\Validate;
// use app\api\validate\TestValidate;
use app\api\validate\IDMustBePostiveInt;
use app\api\model\Banner as BannerModel;
use app\lib\exception\BannerMissException;
// use think\Exception;

/**
 * 轮播图管理
 */
class Banner
{
	/**
	 * 获取指定的banner信息
	 * @url    /banner/:id
	 * @http   get
	 * @param  [int] $id [banner id ]
	 * @return [json]     [description]
	 */
	public function getBanner($id) 
	{

		/**	
			$validate = new IDMustBePostiveInt();
			$res = $validate->goCheck();
		*/
		// 简写
		(new IDMustBePostiveInt())->goCheck();
		
		$banner = BannerModel::getBannerById($id);
		var_dump($banner);
		if (!$banner) {
			throw New BannerMissException();
			// throw New Exception("错误日志...");
		}
	}
	
	// 简单的使用一下 验证期
	public function demo3($id) 
	{
		echo $id;
		$data = [
			'id'  => $id
		];
		$validate = new IDMustBePostiveInt();
		$res = $validate->batch()->check($data);
		dump($validate->getError());
		echo $res;
	}

	//  验证器
	public function demo2($id) 
	{
		$data = [
			'name'  => 'vendor123',
			'email' => '123qq.com'
		];
		$validate = new TestValidate();
		$res = $validate->batch()->check($data); // 检测所有错误 -- 批量验证
		dump($validate->getError());
	}
	
	// 独立验证器
	public function demo($id) 
	{
		/* validate 独立验证 验证器 */
		$data = [
			'name'  => 'vendor123',
			'email' => '123qq.com'
		];

		$validate = new Validate([
			'name' => 'require|max:6',
			'email' => 'email'
		]);

		// $res = $validate->check($data); // 有一个不符合就会报错，停止
		$res = $validate->batch()->check($data); // 检测所有错误 -- 批量验证

		var_dump($res);
		dump($validate->getError());
	}
	
}